package cn.zhpdmmz.service.impl;

import cn.zhpdmmz.entity.CheckInformation;
import cn.zhpdmmz.entity.HotelRoom;
import cn.zhpdmmz.entity.Reservation;
import cn.zhpdmmz.entity.Settlement;
import cn.zhpdmmz.mapper.*;
import cn.zhpdmmz.service.ISettlementService;
import cn.zhpdmmz.utils.DateUtils;
import cn.zhpdmmz.vo.SettlementVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Settlement)表服务实现类
 *
 * @author CJW
 * @since 2022-04-18 20:53:12
 */
@Service("settlementService")
public class SettlementServiceImpl implements ISettlementService {
    @Resource
    private SettlementMapper settlementMapper;

    private HotelRoom hotelRoom;

    private Settlement settlement;

    @Resource
    private HotelRoomMapper hotelRoomMapper;

    @Resource
    private CheckInformationMapper checkInformationMapper;

    @Resource
    private PaymentMapper paymentMapper;
    @Resource
    private RoomStatusMapper roomStatusMapper;
    @Resource
    private HotelRoomTypeMapper hotelRoomTypeMapper;
    @Resource
    private  ReservationMapper reservationMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param settlementId 主键
     * @return 实例对象
     */
    @Override
    public Settlement queryById(Integer settlementId) {
        return this.settlementMapper.queryById(settlementId);
    }

    /**
     * 查询指定行
     *
     * @param settlement 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Settlement> queryAll(Settlement settlement) {
        return this.settlementMapper.queryAll(settlement);
    }

    /**
     * 新增数据
     *
     * @param settlement 实例对象
     * @return 实例对象
     */
    @Override
    public Settlement insert(Settlement settlement) {
        this.settlementMapper.insert(settlement);
        return settlement;
    }

    /**
     * 修改数据
     *
     * @param settlement 实例对象
     * @return 实例对象
     */
    @Override
    public Settlement update(Settlement settlement) {
        this.settlementMapper.update(settlement);
        return this.queryById(settlement.getSettlementId());
    }

    /**
     * 通过主键删除数据
     *
     * @param settlementId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer settlementId) {
        return this.settlementMapper.deleteById(settlementId) > 0;
    }

    /**
     * 结算订单
     *
     * @param settlementVo
     * @return 实例对象
     */
    @Override
    public boolean settlementByRoomNo(SettlementVo settlementVo){
        int statusId = hotelRoomMapper.roomStatusQueryByRoomNo(settlementVo.getRoomNo()).getStatusId();
        if(roomStatusMapper.queryById(statusId).getStatusName().equals("已入住")){

            //修改房间状态
            hotelRoom = new HotelRoom();
            hotelRoom.setRoomId(hotelRoomMapper.roomStatusQueryByRoomNo(settlementVo.getRoomNo()).getRoomId());
            hotelRoom.setStatusId(3);
            hotelRoomMapper.update(hotelRoom);

            //新建结算表
            settlement = new Settlement();
            //订单信息单id

            settlement.setCheckInformationId(checkInformationMapper.queryNewByRoomNo(settlementVo.getRoomNo()).getCheckInformationId());
            //实际支付金额
            Date reserveTime,nowTime;
            CheckInformation checkInformation;
            checkInformation = checkInformationMapper.queryNewByRoomNo(settlementVo.getRoomNo());
            nowTime = checkInformation.getStartTime();
            Reservation reservation;
            reservation = reservationMapper.queryById(1);
            reserveTime = reservation.getStartTime();
            if(DateUtils.getDateDayPoor(reserveTime,nowTime)==0){
                settlement.setActualTotalAmount(reservation.getTotalAmount());
            }
            else{
                settlement.setActualTotalAmount(paymentMapper.queryByCheckInformationId(checkInformationMapper.queryNewByRoomNo(settlementVo.getRoomNo()).getCheckInformationId()).getActualTotalAmount());
            }
            //退房时间
            settlement.setCheckoutTime(DateUtils.getNowDate());
            //退还金额
            long day = DateUtils.getDateDayPoor(checkInformationMapper.queryNewByRoomNo(settlementVo.getRoomNo()).getStartTime(),DateUtils.getNowDate());
            if(day > 1){
                settlement.setRefundAmount(day * hotelRoomTypeMapper.queryById(hotelRoomMapper.roomStatusQueryByRoomNo(settlementVo.getRoomNo()).getRoomTypeId()).getPrice());
            }
            else{
                settlement.setRefundAmount(0.0);
            }

            //添加信息
            settlementMapper.insert(settlement);

            return true;
        }
        else{
            return false;
        }

    }
}
